using System;
using System.Data;
using System.Windows.Forms;

namespace TX
{
    public partial class Repository : Base2
    {
        public Repository()
        {
            InitializeComponent();
        }

        string ev = null;
        TX.Data.DSrep dsrep = null;
        TX.Data.DSrepTableAdapters.AddressTypeTableAdapter AddressTypeTableAdapter = null;
        TX.Data.DSrepTableAdapters.AlisSekliTableAdapter AlisSekliTableAdapter = null;
        TX.Data.DSrepTableAdapters.BelgeTipiTableAdapter BelgeTipiTableAdapter = null;
        TX.Data.DSrepTableAdapters.CarrierTableAdapter CarrierTableAdapter = null;
        TX.Data.DSrepTableAdapters.ContactTableAdapter ContactTableAdapter= null;
        TX.Data.DSrepTableAdapters.ContactTypeTableAdapter ContactTypeTableAdapter = null;
        TX.Data.DSrepTableAdapters.CountryTableAdapter CountryTableAdapter = null;
        TX.Data.DSrepTableAdapters.CultureTableAdapter CultureTableAdapter = null;
        TX.Data.DSrepTableAdapters.CurrencyTableAdapter CurrencyTableAdapter = null;
        TX.Data.DSrepTableAdapters.CustomerTableAdapter CustomerTableAdapter = null;
        TX.Data.DSrepTableAdapters.CustomerBrandTableAdapter CustomerBrandTableAdapter = null;
        TX.Data.DSrepTableAdapters.DeliveryTermTableAdapter DeliveryTermTableAdapter = null;
        TX.Data.DSrepTableAdapters.DepartmentTableAdapter DepartmentTableAdapter = null;
        TX.Data.DSrepTableAdapters.EmployeeTableAdapter EmployeeTableAdapter = null;
        TX.Data.DSrepTableAdapters.ExpensePercentageTableAdapter ExpensePercentageTableAdapter = null;
        TX.Data.DSrepTableAdapters.HareketTipiTableAdapter HareketTipiTableAdapter = null;
        TX.Data.DSrepTableAdapters.LocationTableAdapter LocationTableAdapter = null;
        TX.Data.DSrepTableAdapters.OriginTableAdapter OriginTableAdapter = null;
        TX.Data.DSrepTableAdapters.PaymentTermTableAdapter PaymentTermTableAdapter = null;
        TX.Data.DSrepTableAdapters.ProductTableAdapter ProductTableAdapter = null;
        TX.Data.DSrepTableAdapters.ProductCategoryTableAdapter ProductCategoryTableAdapter = null;
        TX.Data.DSrepTableAdapters.ProductSubcategoryTableAdapter ProductSubcategoryTableAdapter = null;
        TX.Data.DSrepTableAdapters.SalesPersonTableAdapter SalesPersonTableAdapter = null;
        TX.Data.DSrepTableAdapters.ShelfTableAdapter ShelfTableAdapter = null;
        TX.Data.DSrepTableAdapters.ShipMethodTableAdapter ShipMethodTableAdapter = null;
        TX.Data.DSrepTableAdapters.StokkoduTipiTableAdapter StokkoduTipiTableAdapter = null;
        TX.Data.DSrepTableAdapters.UnitMeasureTableAdapter UnitMeasureTableAdapter = null;
        TX.Data.DSrepTableAdapters.VendorTableAdapter VendorTableAdapter = null;
        TX.Data.DSrepTableAdapters.SalesAgentTableAdapter SalesAgentTableAdapter = null;

        TX.Data.DSrepTableAdapters.LookupTablesTableAdapter LookupTablesTableAdapter = null;

        private void Repository_Load(object sender, EventArgs e)
        {
            try
            {
                dsrep = new TX.Data.DSrep();

                LookupTablesTableAdapter = new TX.Data.DSrepTableAdapters.LookupTablesTableAdapter();
                LookupTablesTableAdapter.Fill(dsrep.LookupTables);

                treeList1.OptionsView.ShowHorzLines = false;
                treeList1.OptionsView.ShowIndicator = false;
                treeList1.OptionsView.ShowVertLines = false;
                treeList1.OptionsView.ShowColumns = false;

                treeList1.ParentFieldName = "ParentNode";
                treeList1.KeyFieldName = "TableName";
                treeList1.DataSource = dsrep.LookupTables;
                treeList1.PopulateColumns();
                treeList1.Columns["Table2Name"].VisibleIndex = -1;
                treeList1.Columns["TableName"].OptionsColumn.AllowEdit = false;
                treeList1.Columns["TableName"].OptionsColumn.AllowFocus = false;
                //treeList1.OptionsSelection.EnableAppearanceFocusedCell = false;
                //treeList1.OptionsSelection.EnableAppearanceFocusedRow = false;

                //treeList1.Appearance.FocusedRow.Options.UseFont = true;
                treeList1.ExpandAll();
            }
            catch (Exception Ex)
            {
                //throw;
            }

        }

        private void TableNameChanged(string ev)
        {
            try
            {
                switch (ev)
                {
                    case "AddressType":
                        AddressTypeTableAdapter = new TX.Data.DSrepTableAdapters.AddressTypeTableAdapter();
                        AddressTypeTableAdapter.ClearBeforeFill = true;
                        AddressTypeTableAdapter.Fill(dsrep.AddressType);
                        break;

                    case "AlisSekli":
                        AlisSekliTableAdapter = new TX.Data.DSrepTableAdapters.AlisSekliTableAdapter();
                        AlisSekliTableAdapter.ClearBeforeFill = true;
                        AlisSekliTableAdapter.Fill(dsrep.AlisSekli);
                        break;

                    case "BelgeTipi":
                        BelgeTipiTableAdapter = new TX.Data.DSrepTableAdapters.BelgeTipiTableAdapter();
                        BelgeTipiTableAdapter.ClearBeforeFill = true;
                        BelgeTipiTableAdapter.Fill(dsrep.BelgeTipi);
                        break;

                    case "Carrier":
                        CarrierTableAdapter = new TX.Data.DSrepTableAdapters.CarrierTableAdapter();
                        CarrierTableAdapter.ClearBeforeFill = true;
                        CarrierTableAdapter.Fill(dsrep.Carrier);
                        break;

                    case "Contact":
                        ContactTableAdapter = new TX.Data.DSrepTableAdapters.ContactTableAdapter();
                        ContactTableAdapter.ClearBeforeFill = true;
                        ContactTableAdapter.Fill(dsrep.Contact);
                        break;

                    case "ContactType":
                        ContactTypeTableAdapter = new TX.Data.DSrepTableAdapters.ContactTypeTableAdapter();
                        ContactTypeTableAdapter.ClearBeforeFill = true;
                        ContactTypeTableAdapter.Fill(dsrep.ContactType);
                        break;

                    case "Country":
                        CountryTableAdapter = new TX.Data.DSrepTableAdapters.CountryTableAdapter();
                        CountryTableAdapter.ClearBeforeFill = true;
                        CountryTableAdapter.Fill(dsrep.Country);
                        break;

                    case "Culture":
                        CultureTableAdapter = new TX.Data.DSrepTableAdapters.CultureTableAdapter();
                        CultureTableAdapter.ClearBeforeFill = true;
                        CultureTableAdapter.Fill(dsrep.Culture);
                        break;

                    case "Currency":
                        CurrencyTableAdapter = new TX.Data.DSrepTableAdapters.CurrencyTableAdapter();
                        CurrencyTableAdapter.ClearBeforeFill = true;
                        CurrencyTableAdapter.Fill(dsrep.Currency);
                        break;

                    case "Customer":
                        CustomerTableAdapter = new TX.Data.DSrepTableAdapters.CustomerTableAdapter();
                        CustomerTableAdapter.ClearBeforeFill = true;
                        CustomerTableAdapter.Fill(dsrep.Customer);
                        break;

                    case "CustomerBrand":
                        CustomerBrandTableAdapter = new TX.Data.DSrepTableAdapters.CustomerBrandTableAdapter();
                        CustomerBrandTableAdapter.ClearBeforeFill = true;
                        CustomerBrandTableAdapter.Fill(dsrep.CustomerBrand);
                        break;

                    case "DeliveryTerm":
                        DeliveryTermTableAdapter = new TX.Data.DSrepTableAdapters.DeliveryTermTableAdapter();
                        DeliveryTermTableAdapter.ClearBeforeFill = true;
                        DeliveryTermTableAdapter.Fill(dsrep.DeliveryTerm);
                        break;

                    case "Department":
                        DepartmentTableAdapter = new TX.Data.DSrepTableAdapters.DepartmentTableAdapter();
                        DepartmentTableAdapter.ClearBeforeFill = true;
                        DepartmentTableAdapter.Fill(dsrep.Department);
                        break;

                    case "Employee":
                        EmployeeTableAdapter = new TX.Data.DSrepTableAdapters.EmployeeTableAdapter();
                        EmployeeTableAdapter.ClearBeforeFill = true;
                        EmployeeTableAdapter.Fill(dsrep.Employee);
                        break;

                    case "ExpensePercentage":
                        ExpensePercentageTableAdapter = new TX.Data.DSrepTableAdapters.ExpensePercentageTableAdapter();
                        ExpensePercentageTableAdapter.ClearBeforeFill = true;
                        ExpensePercentageTableAdapter.Fill(dsrep.ExpensePercentage);
                        break;

                    case "HareketTipi":
                        HareketTipiTableAdapter = new TX.Data.DSrepTableAdapters.HareketTipiTableAdapter();
                        HareketTipiTableAdapter.ClearBeforeFill = true;
                        HareketTipiTableAdapter.Fill(dsrep.HareketTipi);
                        break;

                    case "Location":
                        LocationTableAdapter = new TX.Data.DSrepTableAdapters.LocationTableAdapter();
                        LocationTableAdapter.ClearBeforeFill = true;
                        LocationTableAdapter.Fill(dsrep.Location);
                        break;

                    case "Origin":
                        OriginTableAdapter = new TX.Data.DSrepTableAdapters.OriginTableAdapter();
                        OriginTableAdapter.ClearBeforeFill = true;
                        OriginTableAdapter.Fill(dsrep.Origin);
                        break;

                    case "PaymentTerm":
                        PaymentTermTableAdapter = new TX.Data.DSrepTableAdapters.PaymentTermTableAdapter();
                        PaymentTermTableAdapter.ClearBeforeFill = true;
                        PaymentTermTableAdapter.Fill(dsrep.PaymentTerm);
                        break;

                    case "Product":
                        ProductTableAdapter = new TX.Data.DSrepTableAdapters.ProductTableAdapter();
                        ProductTableAdapter.ClearBeforeFill = true;
                        ProductTableAdapter.Fill(dsrep.Product);
                        break;

                    case "ProductCategory":
                        ProductCategoryTableAdapter = new TX.Data.DSrepTableAdapters.ProductCategoryTableAdapter();
                        ProductCategoryTableAdapter.ClearBeforeFill = true;
                        ProductCategoryTableAdapter.Fill(dsrep.ProductCategory);
                        break;

                    case "ProductSubcategory":
                        ProductSubcategoryTableAdapter = new TX.Data.DSrepTableAdapters.ProductSubcategoryTableAdapter();
                        ProductSubcategoryTableAdapter.ClearBeforeFill = true;
                        ProductSubcategoryTableAdapter.Fill(dsrep.ProductSubcategory);
                        break;

                    case "SalesPerson":
                        SalesPersonTableAdapter = new TX.Data.DSrepTableAdapters.SalesPersonTableAdapter();
                        SalesPersonTableAdapter.ClearBeforeFill = true;
                        SalesPersonTableAdapter.Fill(dsrep.SalesPerson);
                        break;

                    case "Shelf":
                        ShelfTableAdapter = new TX.Data.DSrepTableAdapters.ShelfTableAdapter();
                        ShelfTableAdapter.ClearBeforeFill = true;
                        ShelfTableAdapter.Fill(dsrep.Shelf);
                        break;

                    case "ShipMethod":
                        ShipMethodTableAdapter = new TX.Data.DSrepTableAdapters.ShipMethodTableAdapter();
                        ShipMethodTableAdapter.ClearBeforeFill = true;
                        ShipMethodTableAdapter.Fill(dsrep.ShipMethod);
                        break;

                    case "StokkoduTipi":
                        StokkoduTipiTableAdapter = new TX.Data.DSrepTableAdapters.StokkoduTipiTableAdapter();
                        StokkoduTipiTableAdapter.ClearBeforeFill = true;
                        StokkoduTipiTableAdapter.Fill(dsrep.StokkoduTipi);
                        break;

                    case "UnitMeasure":
                        UnitMeasureTableAdapter = new TX.Data.DSrepTableAdapters.UnitMeasureTableAdapter();
                        UnitMeasureTableAdapter.ClearBeforeFill = true;
                        UnitMeasureTableAdapter.Fill(dsrep.UnitMeasure);
                        break;

                    case "Vendor":
                        VendorTableAdapter = new TX.Data.DSrepTableAdapters.VendorTableAdapter();
                        VendorTableAdapter.ClearBeforeFill = true;
                        VendorTableAdapter.Fill(dsrep.Vendor);
                        break;

                    case "SalesAgent":
                        SalesAgentTableAdapter = new TX.Data.DSrepTableAdapters.SalesAgentTableAdapter();
                        SalesAgentTableAdapter.ClearBeforeFill = true;
                        SalesAgentTableAdapter.Fill(dsrep.SalesAgent);
                        break;
                }


                bs1 = new BindingSource(dsrep, ev);

                gridView1.BeginUpdate();
                bindingNavigator1.BindingSource = bs1;
                gridView1.Columns.Clear();
                gridControl1.DataSource = bs1;
                InitGridView(gridView1, true);
                gridView1.BestFitColumns();

                //this.gridView1.OptionsBehavior.Editable = false;
                //this.gridView1.OptionsView.ColumnAutoWidth = false;
                //this.gridView1.OptionsView.EnableAppearanceOddRow = true;
                //this.gridView1.OptionsView.ShowAutoFilterRow = true;
                //gridView1.UnselectRow(1);
                //this.gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
                gridView1.EndUpdate();


            }
            catch (Exception Ex)
            {
                //throw;
            }
        }


        public override void Kaydet()
        {
            DataRow[] rowsToSubmit;

            switch (ev)
            {
                case "AddressType":
                    rowsToSubmit = dsrep.AddressType.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.AddressTypeTableAdapter.Update(rowsToSubmit);
                    break;

                case "AlisSekli":
                    rowsToSubmit = dsrep.AlisSekli.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.AlisSekliTableAdapter.Update(rowsToSubmit);
                    break;

                case "BelgeTipi":
                    rowsToSubmit = dsrep.BelgeTipi.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.BelgeTipiTableAdapter.Update(rowsToSubmit);
                    break;

                case "Carrier":
                    rowsToSubmit = dsrep.Carrier.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.CarrierTableAdapter.Update(rowsToSubmit);
                    break;

                case "Contact":
                    rowsToSubmit = dsrep.Contact.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ContactTableAdapter.Update(rowsToSubmit);
                    break;

                case "ContactType":
                    rowsToSubmit = dsrep.ContactType.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ContactTypeTableAdapter.Update(rowsToSubmit);
                    break;

                case "Country":
                    rowsToSubmit = dsrep.Country.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.CountryTableAdapter.Update(rowsToSubmit);
                    break;

                case "Culture":
                    rowsToSubmit = dsrep.Culture.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.CultureTableAdapter.Update(rowsToSubmit);
                    break;

                case "Currency":
                    rowsToSubmit = dsrep.Currency.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.CurrencyTableAdapter.Update(rowsToSubmit);
                    break;

                case "Customer":
                    rowsToSubmit = dsrep.Customer.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.CustomerTableAdapter.Update(rowsToSubmit);
                    break;

                case "CustomerBrand":
                    rowsToSubmit = dsrep.CustomerBrand.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.CustomerBrandTableAdapter.Update(rowsToSubmit);
                    break;

                case "DeliveryTerm":
                    rowsToSubmit = dsrep.DeliveryTerm.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.DeliveryTermTableAdapter.Update(rowsToSubmit);
                    break;

                case "Department":
                    rowsToSubmit = dsrep.Department.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.DepartmentTableAdapter.Update(rowsToSubmit);
                    break;

                case "Employee":
                    rowsToSubmit = dsrep.Employee.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.EmployeeTableAdapter.Update(rowsToSubmit);
                    break;

                case "ExpensePercentage":
                    rowsToSubmit = dsrep.ExpensePercentage.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ExpensePercentageTableAdapter.Update(rowsToSubmit);
                    break;

                case "HareketTipi":
                    rowsToSubmit = dsrep.HareketTipi.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.HareketTipiTableAdapter.Update(rowsToSubmit);
                    break;

                case "Location":
                    rowsToSubmit = dsrep.Location.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.LocationTableAdapter.Update(rowsToSubmit);
                    break;

                case "Origin":
                    rowsToSubmit = dsrep.Origin.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.OriginTableAdapter.Update(rowsToSubmit);
                    break;

                case "PaymentTerm":
                    rowsToSubmit = dsrep.PaymentTerm.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.PaymentTermTableAdapter.Update(rowsToSubmit);
                    break;

                case "Product":
                    rowsToSubmit = dsrep.Product.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ProductTableAdapter.Update(rowsToSubmit);
                    break;

                case "ProductCategory":
                    rowsToSubmit = dsrep.ProductCategory.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ProductCategoryTableAdapter.Update(rowsToSubmit);
                    break;

                case "ProductSubcategory":
                    rowsToSubmit = dsrep.ProductSubcategory.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ProductSubcategoryTableAdapter.Update(rowsToSubmit);
                    break;

                case "SalesPerson":
                    rowsToSubmit = dsrep.SalesPerson.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.SalesPersonTableAdapter.Update(rowsToSubmit);
                    break;

                case "Shelf":
                    rowsToSubmit = dsrep.Shelf.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ShelfTableAdapter.Update(rowsToSubmit);
                    break;

                case "ShipMethod":
                    rowsToSubmit = dsrep.ShipMethod.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.ShipMethodTableAdapter.Update(rowsToSubmit);
                    break;

                case "StokkoduTipi":
                    rowsToSubmit = dsrep.StokkoduTipi.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.StokkoduTipiTableAdapter.Update(rowsToSubmit);
                    break;

                case "UnitMeasure":
                    rowsToSubmit = dsrep.UnitMeasure.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.UnitMeasureTableAdapter.Update(rowsToSubmit);
                    break;

                case "Vendor":
                    rowsToSubmit = dsrep.Vendor.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.VendorTableAdapter.Update(rowsToSubmit);
                    break;

                case "SalesAgent":
                    rowsToSubmit = dsrep.SalesAgent.Select("", "", Added | Deleted);
                    if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.SalesAgentTableAdapter.Update(rowsToSubmit);
                    break;

            }

            int pos1 = bs1.Position;
            dsrep.AcceptChanges();
            bs1.Position = pos1;
        }




        private void ShowHitInfo(DevExpress.XtraTreeList.TreeListHitInfo hi)
        {
            if (!(hi.Column == null || hi.Node == null))
                TableNameChanged(hi.Node.GetDisplayText(hi.Column.AbsoluteIndex));
        }

        private void treeList1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //ShowHitInfo(treeList1.CalcHitInfo(new Point(e.X, e.Y)));
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (treeList1.Selection[0] != null)
            {
                ev = treeList1.Selection[0].GetValue(0).ToString();
                TableNameChanged(ev);
            }
        }




    }
}